/**
 *   
 * Copyright (c) 2010-2020 Founder Ltd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * Founder. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with Founder.   
 *   
 *
 * @Project android_api
 * @Package com.example.android_api.util
 * @Title WebServiceUtil.java
 * @Description 
 * @Author robinjiang
 * @Date 2013-11-1 下午5:15:05
 * @Version V1.0
 */
package com.example.android_api.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * 
 * @Project_Name android_api
 * @Type_Name WebServiceUtil
 * @Description
 * @Author robinjiang
 * @Date 2013-11-1 下午5:15:05
 * 
 * @Update_User robinjiang
 * @Update_Date 2013-11-1 下午5:15:05
 * @Update_Desprition
 * 
 * @Version V1.0
 */
public class WebServiceUtil {

	/**
	 * @Description
	 * @Author robinjiang
	 * @Date 2013-11-1 下午5:15:05
	 * 
	 * @Update_User robinjiang
	 * @Update_Date 2013-11-1 下午5:15:05
	 * @Update_Desprition
	 * 
	 */
	public WebServiceUtil() {
		// TODO Auto-generated constructor stub
	}

	public static void connectUrl() {

	}

	public static boolean checkUser(String username,String pass,String companyId,HashMap<String, String> session) {
		DefaultHttpClient mHttpClient = new DefaultHttpClient();
		HttpPost mPost = new HttpPost("http://192.168.0.134:8080/twp/login");
//		HttpPost mPost = new HttpPost("http://192.168.0.134:8080/twp");
		// 传递用户名和密码相当于
		// http://10.0.2.2/web/php/login.php?username=''&password=''
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		
		pairs.add(new BasicNameValuePair("username", username+":"+companyId));
		pairs.add(new BasicNameValuePair("password", pass));
		try {
			mPost.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//密码错误暂未解决。。。
		try {
			HttpResponse response = mHttpClient.execute(mPost);
			int res = response.getStatusLine().getStatusCode();
			if (res == 200) {
				HttpEntity entity = response.getEntity();
				if (entity != null) {
					String info = EntityUtils.toString(entity);
					return true;
					/*System.out.println("info-----------" + info);
					// 以下主要是对服务器端返回的数据进行解析
					JSONObject jsonObject = null;
					// flag为登录成功与否的标记,从服务器端返回的数据
					String flag = "";
					String name = "";
					String userid = "";
					String sessionid = "";
					try {
						jsonObject = new JSONObject(info);
						flag = jsonObject.getString("flag");
						name = jsonObject.getString("name");
						userid = jsonObject.getString("userid");
						sessionid = jsonObject.getString("sessionid");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// 根据服务器端返回的标记,判断服务端端验证是否成功
					if (flag.equals("success")) {
						// 为session传递相应的值,用于在session过程中记录相关用户信息
						session.put("s_userid", userid);
						session.put("s_username", name);
						session.put("s_sessionid", sessionid);
						return true;
					} else {
						return false;
					}*/
				} else {
					return false;
				}
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
